using System;
using System.Xml;

namespace JamesKovacs.ReSharper.UnitTestSupport.VstsUnit {
    [Serializable]
    public class VstsUnitTestTask : VstsUnitTestTaskBase, IEquatable<VstsUnitTestTask> {
        private readonly string m_TestMethod;
        private readonly string m_TestType;

        public VstsUnitTestTask(XmlElement element) : base(element) {
            m_TestMethod = GetXmlAttribute(element, "TestMethod");
            m_TestType = GetXmlAttribute(element, "TestType");
            m_Explicitly = GetXmlAttribute(element, "Explicitly") == "true";
        }

        public VstsUnitTestTask(string testType, string testMethod, bool explicitly) : base("VstsUnit") {
            if(testMethod == null) {
                throw new ArgumentNullException("testMethod");
            }
            if(testType == null) {
                throw new ArgumentNullException("testType");
            }
            m_TestType = testType;
            m_TestMethod = testMethod;
            m_Explicitly = explicitly;
        }

        public bool Equals(VstsUnitTestTask vstsUnitTestTask) {
            if(vstsUnitTestTask != null) {
                if(!base.Equals(vstsUnitTestTask)) {
                    return false;
                }
                if(Equals(m_TestType, vstsUnitTestTask.m_TestType) && Equals(m_TestMethod, vstsUnitTestTask.m_TestMethod)) {
                    return (m_Explicitly == vstsUnitTestTask.m_Explicitly);
                }
            }
            return false;
        }

        public override bool Equals(object obj) {
            return ((this == obj) || Equals(obj as VstsUnitTestTask));
        }

        public override int GetHashCode() {
            int result = base.GetHashCode();
            result = (0x1d*result) + m_TestType.GetHashCode();
            result = (0x1d*result) + m_TestMethod.GetHashCode();
            return ((0x1d*result) + m_Explicitly.GetHashCode());
        }

        public override void SaveXml(XmlElement element) {
            base.SaveXml(element);
            SetXmlAttribute(element, "TestMethod", TestMethod);
            SetXmlAttribute(element, "TestType", TestType);
            SetXmlAttribute(element, "Explicitly", Explicitly ? "true" : "false");
        }

        public string TestMethod {
            get { return m_TestMethod; }
        }

        public string TestType {
            get { return m_TestType; }
        }

        public override string[] MethodNames {
            get { return new string[] {m_TestMethod}; }
        }
    }
}